/****************************************************************************
	
	Date: October, 2017
	
	Program: Model Calibration
	
	This file:
	- Organizes the 2012 WIOT into a form usable for the calibration and 
	produces sectoral labor shares.
	
	Data to put in $datapathWIOT: 
	- WIOT12.dta
	- WIOT_alpha.dta
	
	Output: 
	- WIOT_Calibration_Wide
	- WIOT_For_Comparison_Wide
		
****************************************************************************/

tempfile f
di "`f'"


cd "$wiotdata"


*** +++ Load WIOD Table, Rename Variables, Harmoise Sectors & Countries +++ ***

use WIOT12, clear

rename row_country Exporter
rename col_country Importer
rename value Value
gen str9 wiot12_jp ="."
gen str9 wiot12_exporter ="."
rename col_item wiot12_jp_item
rename row_item wiot12_exporter_item
local vars wiot12_jp wiot12_exporter
foreach var of local vars {
	replace `var' = "AtB" if `var'_item==1
	replace `var' = "C" if `var'_item==2
	replace `var' = "15t16" if `var'_item==3
	replace `var' = "17t18" if `var'_item==4
	replace `var' = "19" if `var'_item==5
	replace `var' = "20" if `var'_item==6
	replace `var' = "21t22" if `var'_item==7
	replace `var' = "23" if `var'_item==8
	replace `var' = "24" if `var'_item==9
	replace `var' = "25" if `var'_item==10
	replace `var' = "26" if `var'_item==11
	replace `var' = "27t28" if `var'_item==12
	replace `var' = "29" if `var'_item==13
	replace `var' = "30t33" if `var'_item==14
	replace `var' = "34t35" if `var'_item==15
	replace `var' = "36t37" if `var'_item==16
	replace `var' = "E" if `var'_item==17
	replace `var' = "F" if `var'_item==18
	replace `var' = "50" if `var'_item==19
	replace `var' = "51" if `var'_item==20
	replace `var' = "52" if `var'_item==21
	replace `var' = "H" if `var'_item==22
	replace `var' = "60" if `var'_item==23
	replace `var' = "61" if `var'_item==24
	replace `var' = "62" if `var'_item==25
	replace `var' = "63" if `var'_item==26
	replace `var' = "64" if `var'_item==27
	replace `var' = "J" if `var'_item==28
	replace `var' = "70" if `var'_item==29
	replace `var' = "71t74" if `var'_item==30
	replace `var' = "L" if `var'_item==31
	replace `var' = "M" if `var'_item==32
	replace `var' = "N" if `var'_item==33
	replace `var' = "O" if `var'_item==34
	replace `var' = "P" if `var'_item==35
}
local vars wiot12_jp wiot12_exporter
foreach var of local vars {
	drop if `var'=="." | `var'=="J" | `var'=="P" | `var'=="L"
}
drop if Exporter=="GO" | Exporter=="VA"
replace Importer="BEL" if Importer=="LUX"
replace Exporter="BEL" if Exporter=="LUX"
gen ROW=0
gen ROW_2=0
foreach i in $wiotcountry_FRA{
	replace ROW=1 if Importer=="`i'"
	replace ROW_2=1 if Exporter=="`i'"
}
replace Importer="ROW" if ROW!=1
replace Exporter="ROW" if ROW_2!=1
replace Importer="ROW" if Importer=="RoW"
replace Exporter="ROW" if Exporter=="RoW"
compress
gcollapse (sum) Value, by(year Exporter Importer wiot12_jp wiot12_exporter)
replace year = year+1
cd "$intermediatesdata"
save WIOT_Hold, replace



*** +++ Fit Tradability Assumptions +++ ***

keep if Importer=="FRA" 
bysort year wiot12_jp wiot12_exporter: egen total_foreign = sum(Value)
foreach i in $wiotsector_NT_Only{
	replace Value=0 if Importer=="FRA" & Exporter!="FRA" & wiot12_exporter=="`i'"
	replace Value=total_foreign if Importer=="FRA" & Exporter=="FRA" & wiot12_exporter=="`i'"
}
save WIOT_Hold, replace
drop if Exporter!="FRA"
foreach i in $wiotsector{
	drop if wiot12_exporter=="`i'"
}
collapse (sum) Value, by(year Importer wiot12_jp)
rename Value Total_NT_Use
save Total_NT_Use, replace

use WIOT_Hold, clear
merge m:1 year Importer wiot12_jp using Total_NT_Use, nogenerate
erase Total_NT_Use.dta

drop total_foreign
egen id_country=group(Importer)
egen id_industry=group(wiot12_jp)
bysort id_country id_industry year: egen Total=sum(Value)
gen gamma_WIOT=Value/Total
gen gamma_NT=Total_NT_Use/Total
gen gamma_TR=1-gamma_NT
bysort id_country id_industry year: egen Check=sum(gamma_WIOT)
sum Check, d
drop Check
save WIOT_Hold, replace
keep if Importer=="FRA" & Exporter=="FRA"
gen Keeper=0
foreach i in $wiotsector{
	replace Keeper=1 if wiot12_exporter=="`i'" & Exporter=="FRA"
}
drop if Keeper!=1
bysort id_industry year: egen gamma_TR_FRA=sum(gamma_WIOT)
keep year Exporter Importer wiot12_jp wiot12_exporter gamma_TR_FRA
save WIOT_TR_FRA, replace

use WIOT_Hold, clear
merge 1:1 year Exporter Importer wiot12_jp wiot12_exporter using WIOT_TR_FRA, nogenerate

replace gamma_TR_FRA=0 if gamma_TR_FRA==.
egen: Imports_From = concat(Exporter wiot12_exporter)
drop id_country Total_NT_Use Value Total
order year Importer wiot12_jp Exporter wiot12_exporter Imports_From gamma_WIOT gamma_NT gamma_TR
sort Importer wiot12_jp year
drop Exporter wiot12_exporter gamma_TR_FRA
rename gamma_WIOT WIOT
reshape wide WIOT, i(year Importer wiot12_jp) j(Imports_From) string
order  year Importer wiot12_jp gamma_NT gamma_TR
save WIOT_Hold, replace
keep year wiot12_jp *FRA*
gen gamma_TR_domestic = 0
foreach i in $wiotsector{
	replace gamma_TR_domestic=gamma_TR_domestic+WIOTFRA`i'
} 
keep year wiot12_jp gamma_TR_domestic
save WIOT_TR_FRA, replace

use WIOT_Hold, clear
merge 1:1 year wiot12_jp using WIOT_TR_FRA, nogenerate
saveold WIOT_Calibration_Wide2, replace version(13)
erase WIOT_TR_FRA.dta
erase WIOT_Hold.dta


*** +++ Calculate Alphas +++ ***

use "$wiotdata/wiot_full.dta", clear

keep if row_item==64 | row_item==69
keep if col_country=="FRA"
replace year = year+1
gen str9 wiot12_jp ="."
rename col_item wiot12_jp_item
local vars wiot12_jp
foreach var of local vars {
	replace `var' = "AtB" if `var'_item==1
	replace `var' = "C" if `var'_item==2
	replace `var' = "15t16" if `var'_item==3
	replace `var' = "17t18" if `var'_item==4
	replace `var' = "19" if `var'_item==5
	replace `var' = "20" if `var'_item==6
	replace `var' = "21t22" if `var'_item==7
	replace `var' = "23" if `var'_item==8
	replace `var' = "24" if `var'_item==9
	replace `var' = "25" if `var'_item==10
	replace `var' = "26" if `var'_item==11
	replace `var' = "27t28" if `var'_item==12
	replace `var' = "29" if `var'_item==13
	replace `var' = "30t33" if `var'_item==14
	replace `var' = "34t35" if `var'_item==15
	replace `var' = "36t37" if `var'_item==16
	replace `var' = "E" if `var'_item==17
	replace `var' = "F" if `var'_item==18
	replace `var' = "50" if `var'_item==19
	replace `var' = "51" if `var'_item==20
	replace `var' = "52" if `var'_item==21
	replace `var' = "H" if `var'_item==22
	replace `var' = "60" if `var'_item==23
	replace `var' = "61" if `var'_item==24
	replace `var' = "62" if `var'_item==25
	replace `var' = "63" if `var'_item==26
	replace `var' = "64" if `var'_item==27
	replace `var' = "J" if `var'_item==28
	replace `var' = "70" if `var'_item==29
	replace `var' = "71t74" if `var'_item==30
	replace `var' = "L" if `var'_item==31
	replace `var' = "M" if `var'_item==32
	replace `var' = "N" if `var'_item==33
	replace `var' = "O" if `var'_item==34
	replace `var' = "P" if `var'_item==35
}
foreach var of local vars {
	drop if `var'=="." | `var'=="J" | `var'=="P" | `var'=="L"
}
rename col_country country
drop row_item wiot12_jp_item
reshape wide value, i(year country wiot12_jp) j(row_country) string
gen alpha_wiot =  valueVA/valueGO
drop valueGO valueVA
saveold WIOT_alpha, replace version(13)
//erase WIOT_For_Comparison_Wide.dta
